#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/time.h>
#include <pthread.h>
#include "../src/thread.h"

#include"somme.h"

#define nb_thread 4

int main (){


	printf("Avec notre bibliothèque\n\n"); 
	
	int i,err;
	thread_t tid[nb_thread+1];
	void * retval[nb_thread];
	for(i = 0; i < nb_thread ; i++){
		err = thread_create(tid+i,somme, (void*)(i));
		assert(!err);
	printf("fin creation %d\n",i);
	}

	for(i = 0; i < nb_thread; i++){
		err = thread_join(tid[i], &(retval[i]));
		assert(!err);
	printf("fin du join %d\n",i);
	}
	double t1 = getTime();
	printf("%d\n", t1); 
	thread_create(tid+i,somme, (void*)i);
	double t2 = getTime();
	printf("%d\n", t2);
	printf("   1-[Cout de calcul avec thread = %g ms]\n", t2 - t1);
	
	
	
	printf("\nAvec la bibliothèque pthread\n\n"); 
	void * p_retval[nb_thread];
	pthread_t pt_tid[nb_thread+1];
	for(i = 0; i < nb_thread ; i++){
		pthread_create(pt_tid+i,NULL,p_somme, (void*)(i));
	}

	for(i = 0; i < nb_thread; i++){
		err = pthread_join(pt_tid[i], &(p_retval[i]));
		assert(!err);
	}
	double pt_t1 = getTime();
		pthread_create(pt_tid+i,NULL,p_somme,(void*) i);
	double pt_t2 = getTime();
	printf("   1-[Cout de calcul avec pthread = %g ms]\n", pt_t2 - pt_t1);
	
	
	return EXIT_SUCCESS;
}
